4 import java.awt.Dimension;
5 import java.awt.Rectangle;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.FocusEvent;
9 import java.awt.event.FocusListener;
10 import java.awt.event.ItemEvent;
11 import java.awt.event.ItemListener;
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14 import java.rmi.Naming;
15 import java.rmi.RemoteException;
17 import java.text.DateFormat;
18 import java.util.Calendar;
19 import java.util.Locale;
20 import java.util.Vector;
22 import javax.swing.JButton;
23 import javax.swing.JComboBox;
24 import javax.swing.JFrame;
25 import javax.swing.JLabel;
26 import javax.swing.JTextField;
28 import com.toedter.calendar.JCalendar;
29 import common.HouseInterface;
30 import common.OfferInterface;
32 import configuration.___IntNames;
35 import domain.RuralHouse;
36 import exceptions.BadDates;
38 public class ModifyOfferGUI extends JFrame {
40 private static final long serialVersionUID = 1L;
42 private JComboBox<RuralHouse> jComboBox1;
43 private JLabel jLabel1 = new JLabel();
44 private JLabel jLabel2 = new JLabel();
45 private JTextField jTextField1 = new JTextField();
46 private JLabel jLabel3 = new JLabel();
47 private JTextField jTextField2 = new JTextField();
48 private JLabel jLabel4 = new JLabel();
49 private JTextField jTextField3 = new JTextField();
50 private JButton jButton1 = new JButton();
52 private JCalendar jCalendar1 = new JCalendar();
53 private JCalendar jCalendar2 = new JCalendar();
54 private Calendar calendarInicio = null;
55 private Calendar calendarFin = null;
56 private JButton jButton2 = new JButton();
57 private JLabel jLabel5 = new JLabel();
58 private final JLabel jLabel1_o = new JLabel();
59 private JComboBox<Offer> comboBox_o;
60 private Vector<RuralHouse> Hlist = null;
61 private OfferInterface om = null;
62 private HouseInterface hm = null;
65 public ModifyOfferGUI(Owner o) {
68 } catch (Exception e) {
73 private void jbInit(Owner o) throws Exception {
74 this.getContentPane().setLayout(null);
75 this.setSize(new Dimension(513, 433));
76 this.setTitle("Set availability");
80 om = (OfferInterface) Naming
81 .lookup(___IntNames.OfferManager);
82 hm = (HouseInterface) Naming
83 .lookup(___IntNames.HouseManager);
84 } catch (Exception e1) {
85 System.out.println("Error accessing remote authentication: "
89 Hlist = hm.getHouses(o, null, null, 0, 0, 0, 0, 0);
90 } catch (RemoteException e1) {
95 jComboBox1 = new JComboBox<RuralHouse>(Hlist);
97 comboBox_o = new JComboBox<Offer>(
98 ((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers());
99 DateFormat dateformat1 = DateFormat.getDateInstance(1,
100 jCalendar1.getLocale());
101 if (!((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers()
103 jTextField1.setText(dateformat1.format(((Offer) comboBox_o
104 .getSelectedItem()).getFirstDay()));
105 jTextField2.setText(dateformat1.format(((Offer) comboBox_o
106 .getSelectedItem()).getLastDay()));
107 jLabel4.setText(Float.toString(((Offer) comboBox_o
108 .getSelectedItem()).getPrice()));
109 jTextField3.setText(Float.toString(((Offer) comboBox_o
110 .getSelectedItem()).getPrice()));
112 jLabel5.setText("There are no offers for the selected rural house");
113 jCalendar1.setEnabled(false);
114 jCalendar2.setEnabled(false);
115 jButton1.setEnabled(false);
116 comboBox_o.setEnabled(false);
117 jTextField3.setEnabled(false);
119 jComboBox1.setBounds(new Rectangle(115, 12, 115, 20));
120 jLabel1.setText("List of houses:");
121 jLabel1.setBounds(new Rectangle(25, 12, 95, 20));
122 jLabel2.setText("First day :");
123 jLabel2.setBounds(new Rectangle(25, 75, 85, 25));
124 jTextField1.setBounds(new Rectangle(25, 265, 220, 25));
125 jTextField1.setEditable(false);
126 jLabel3.setText("Last day :");
127 jLabel3.setBounds(new Rectangle(260, 75, 75, 25));
128 jTextField2.setBounds(new Rectangle(260, 265, 220, 25));
129 jTextField2.setEditable(false);
130 jLabel4.setText("Price:");
131 jLabel4.setBounds(new Rectangle(260, 30, 75, 20));
132 jTextField3.setBounds(new Rectangle(350, 30, 115, 20));
133 jTextField3.setText("0");
134 jButton1.setText("Accept");
135 jButton1.setBounds(new Rectangle(100, 360, 130, 30));
136 jTextField3.addFocusListener(new FocusListener() {
137 public void focusGained(FocusEvent e) {
140 public void focusLost(FocusEvent e) {
141 jTextField3_focusLost();
145 jComboBox1.addItemListener(new ItemListener() {
148 public void itemStateChanged(ItemEvent arg0) {
150 Vector<Offer> vo= null;
152 vo = om.getRuralHouseOffers((RuralHouse) jComboBox1.getSelectedItem());
153 } catch (RemoteException e) {
156 comboBox_o.removeAllItems();
158 jCalendar1.setEnabled(true);
159 jCalendar2.setEnabled(true);
160 jButton1.setEnabled(true);
161 comboBox_o.setEnabled(true);
162 jTextField3.setEnabled(true);
164 for (Offer of : vo) {
165 comboBox_o.addItem(of);
168 jLabel5.setText("There are no offers for the selected rural house");
169 jCalendar1.setEnabled(false);
170 jCalendar2.setEnabled(false);
171 jButton1.setEnabled(false);
172 comboBox_o.setEnabled(false);
173 jTextField3.setEnabled(false);
180 comboBox_o.addItemListener(new ItemListener() {
183 public void itemStateChanged(ItemEvent arg0) {
184 if (arg0.getStateChange() == ItemEvent.SELECTED) {
185 Offer of = (Offer) comboBox_o.getSelectedItem();
186 DateFormat dateformat1 = DateFormat.getDateInstance(1,
187 jCalendar1.getLocale());
188 jTextField1.setText(dateformat1.format(of.getFirstDay()));
189 jTextField2.setText(dateformat1.format(of.getLastDay()));
195 jButton1.addActionListener(new ActionListener() {
196 public void actionPerformed(ActionEvent e) {
197 jButton1_actionPerformed(e);
200 jButton2.setText("Cancel");
201 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
202 jButton2.addActionListener(new ActionListener() {
203 public void actionPerformed(ActionEvent e) {
204 jButton2_actionPerformed(e);
207 jLabel5.setBounds(new Rectangle(100, 320, 300, 20));
208 jLabel5.setForeground(Color.red);
209 jLabel5.setSize(new Dimension(305, 20));
210 jCalendar1.setBounds(new Rectangle(25, 100, 220, 165));
211 jCalendar2.setBounds(new Rectangle(260, 100, 220, 165));
213 // Code for JCalendar
214 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
215 public void propertyChange(PropertyChangeEvent propertychangeevent) {
216 if (propertychangeevent.getPropertyName().equals("locale")) {
217 jCalendar1.setLocale((Locale) propertychangeevent
219 DateFormat dateformat = DateFormat.getDateInstance(1,
220 jCalendar1.getLocale());
221 jTextField1.setText(dateformat.format(calendarInicio
223 } else if (propertychangeevent.getPropertyName().equals(
225 calendarInicio = (Calendar) propertychangeevent
227 DateFormat dateformat1 = DateFormat.getDateInstance(1,
228 jCalendar1.getLocale());
229 jTextField1.setText(dateformat1.format(calendarInicio
231 jCalendar1.setCalendar(calendarInicio);
236 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener() {
237 public void propertyChange(PropertyChangeEvent propertychangeevent) {
238 if (propertychangeevent.getPropertyName().equals("locale")) {
239 jCalendar2.setLocale((Locale) propertychangeevent
241 DateFormat dateformat = DateFormat.getDateInstance(1,
242 jCalendar2.getLocale());
243 jTextField2.setText(dateformat.format(calendarFin.getTime()));
244 } else if (propertychangeevent.getPropertyName().equals(
246 calendarFin = (Calendar) propertychangeevent.getNewValue();
247 DateFormat dateformat1 = DateFormat.getDateInstance(1,
248 jCalendar2.getLocale());
249 jTextField2.setText(dateformat1.format(calendarFin
251 jCalendar2.setCalendar(calendarFin);
255 if(((Offer) comboBox_o.getSelectedItem()).isBooked()){
256 jLabel5.setText("This offer is already booked.");
257 jButton1.setEnabled(false);
259 comboBox_o.addItemListener(new ItemListener() {
261 public void itemStateChanged(ItemEvent arg0) {
262 if(((Offer) comboBox_o.getSelectedItem()).isBooked()){
263 jLabel5.setText("This offer is already booked.");
264 jButton1.setEnabled(false);
268 comboBox_o.addItemListener(new ItemListener() {
270 public void itemStateChanged(ItemEvent arg0) {
271 if(!((Offer) comboBox_o.getSelectedItem()).isBooked()){
272 jLabel5.setText("");;
273 jButton1.setEnabled(true);
277 this.getContentPane().add(jCalendar2, null);
278 this.getContentPane().add(jCalendar1, null);
279 this.getContentPane().add(jLabel5, null);
280 this.getContentPane().add(jButton2, null);
281 this.getContentPane().add(jButton1, null);
282 this.getContentPane().add(jTextField3, null);
283 this.getContentPane().add(jLabel4, null);
284 this.getContentPane().add(jTextField2, null);
285 this.getContentPane().add(jLabel3, null);
286 this.getContentPane().add(jTextField1, null);
287 this.getContentPane().add(jLabel2, null);
288 this.getContentPane().add(jLabel1, null);
289 this.getContentPane().add(jComboBox1, null);
290 jLabel1_o.setText("List of offers:");
291 jLabel1_o.setBounds(new Rectangle(25, 30, 95, 20));
292 jLabel1_o.setBounds(25, 44, 95, 20);
294 getContentPane().add(jLabel1_o);
295 comboBox_o.setBounds(new Rectangle(115, 30, 115, 20));
296 comboBox_o.setBounds(115, 44, 115, 20);
298 getContentPane().add(comboBox_o);
301 private void jButton1_actionPerformed(ActionEvent e) {
302 RuralHouse ruralHouse = ((RuralHouse) jComboBox1.getSelectedItem());
303 Date firstDay = new Date(jCalendar1.getCalendar().getTime().getTime());
304 // Remove the hour:minute:second:ms from the date
305 firstDay = Date.valueOf(firstDay.toString());
306 Date lastDay = new Date(jCalendar2.getCalendar().getTime().getTime());
307 // Remove the hour:minute:second:ms from the date
308 lastDay = Date.valueOf(lastDay.toString());
313 // It could be to trigger an exception if the introduced string is
315 float price = Float.parseFloat(jTextField3.getText());
317 // Obtain the business logic from a StartWindow class (local or
319 OfferInterface offerM = null;
321 offerM = (OfferInterface) Naming
322 .lookup(___IntNames.OfferManager);
323 } catch (Exception e1) {
324 System.out.println("Error accessing remote authentication: "
328 offerM.deleteOffer(ruralHouse, (Offer) comboBox_o.getSelectedItem());
329 offerM.createOffer(ruralHouse, firstDay, lastDay, price);
331 jLabel5.setText("Offer modified");
333 } catch (java.lang.NumberFormatException e1) {
334 jLabel5.setText(jTextField3.getText() + " is not a valid price");
335 } catch (BadDates e1) {
336 jLabel5.setText("Last day is before first day in the offer");
337 } catch (Exception e1) {
338 e1.printStackTrace();
342 private void jButton2_actionPerformed(ActionEvent e) {
343 this.setVisible(false);
346 private void jTextField3_focusLost() {
348 new Integer(jTextField3.getText());
350 } catch (NumberFormatException ex) {
351 jLabel5.setText("Error: Please introduce a number");